package lime2.ca.plain;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.InetAddress;
import java.net.ServerSocket;
import lime2.LimeServerID;
import lime2.TestOpBuilder;
import lime2.Queue;


public class TestCAUnicast {

	/**
	 * @param args
	 * @author Xing Jiankuan
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		if (args[0].equals("-s")) {
			ServerSocket ssocket = new ServerSocket();
			ssocket.bind(new InetSocketAddress("lenovo-PC", 11115));
			System.out.println("server is waiting at " + ssocket.getInetAddress() + ":" + ssocket.getLocalPort());
			PlainCA.queue = new Queue();
			new PlainUnicastReceiver(ssocket).start();
			new Thread() {
				public void run() {
					while(true) {
						Object obj  = PlainCA.queue.remove();
						System.out.println(obj);
					}
				}
			}.start();
		} else {
			LimeServerID id = new LimeServerID(InetAddress.getByName("192.168.20.112"), 11115);
			java.util.Scanner scanner = new java.util.Scanner(System.in);
			while (true) {
				System.out.print(">");
				String cmd = scanner.next();
				Object op = null;
				if(cmd.equals("exit"))
					break;
				else if(cmd.equals("push_rp2")) {
					op = TestOpBuilder.build("push_rp2", id);
				}
				Connection c = Connection.get(id);
				c.send(op); 
			}
		}
	}
}
